using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using LocalizationTest.Tools;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace LocalizationTest
{
  public class MapWithDepth : Map
  {
    private int[,] depth;
    int widthOfMap;
    int heightOfMap;
  
    public int GetDepth(double xPosition, double yPosition)
    {
      int x = (int)(xPosition + widthOfMap / 2);
      int y = (int)(yPosition + heightOfMap / 2);
      if (
        x >= 0 && x < widthOfMap &&
        y >= 0 && y < heightOfMap)
      {
        return depth[x, y];
      }
      else
      {
        return 0;
      }
    }

    Bitmap bitmap;
    public MapWithDepth()
    {
      string executablePath = Application.ExecutablePath;
      string mapPath = executablePath.Remove(executablePath.IndexOf("\\bin\\")) + "\\Maps\\depth-maps\\";

      bitmap = new Bitmap(mapPath + "depth.png");
      bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
      widthOfMap = bitmap.Width;
      heightOfMap = bitmap.Height;
      CreateDepthArray(bitmap);
    }

    private void CreateDepthArray(Bitmap bitmap)
    {
      depth = new int[bitmap.Width * 2, bitmap.Height * 2];

      BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
      ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
      int stride = bmData.Stride;
      System.IntPtr Scan0 = bmData.Scan0;
      unsafe
      {
        byte* p = (byte*)(void*)Scan0;
        int nOffset = stride - bitmap.Width * 3;
        int nWidth = bitmap.Width * 3;
        for (int y = 0; y < bitmap.Height; ++y)
        {
          for (int x = 0; x < bitmap.Width; ++x)
          {
            Color color = Color.FromArgb(p[0],p[1],p[2]);

//            if (DebugKeyTool.IsActive("options:depth is brightness"))
//              depth[x, y] = (int)(color.R + color.G + color.B) / 3;
//            else
              depth[x, y] = (int)(200 - (color.GetHue() / 360.0 * 250.0) / 163.0 * 200.0);
            if (depth[x, y] < 0) depth[x, y] = 0;
            if (color.R == 255 && color.G == 255 && color.B == 255) depth[x, y] = 0;

            p+=3;
          }
          p += nOffset;
        }
      }

      bitmap.UnlockBits(bmData);

/*
      for (int x = 0; x < bitmap.Width; x++)
      {
        for (int y = 0; y < bitmap.Height; y++)
        {
          Color color = bitmap.GetPixel(x, y);
          if (DebugKeyTool.IsActive("options:depth is brightness"))
            depth[x, y] = (int)(color.R + color.G + color.B) / 3;
          else
            depth[x, y] = (int)(200 - (color.GetHue() / 360.0 * 250.0) / 163.0 * 200.0);
          if (depth[x, y] < 0) depth[x, y] = 0;
          if (color.R == 255 && color.G == 255 && color.B == 255) depth[x, y] = 0;
        }
      }
 */ 
    }


    /*
    public override void Draw(System.Drawing.Graphics graphics)
    {
      //coordinate system
      Pen blackPenThin = new Pen(Color.Black, 1);
      graphics.DrawLine(blackPenThin, -800, 0, 800, 0);
      graphics.DrawLine(blackPenThin, 0, -800, 0, 800);

      if (DebugKeyTool.IsActive("options view:map - hide"))
      {
        graphics.DrawRectangle(blackPenThin, -widthOfMap / 2, -heightOfMap / 2, widthOfMap, heightOfMap);
      }
      else
      {
        graphics.DrawImage(bitmap, -widthOfMap / 2, -heightOfMap / 2);
      }

      if (DebugKeyTool.IsActive("options view:map - pixel wise"))
      {
        for (int x = -500; x < 500; x+=4)
        {
          for (int y = -500; y < 500; y+=4)
          {
            int intensity = (int)GetDepth(x, y);
            if (intensity != 0)
            {
              Pen pen = new Pen(Color.FromArgb(intensity, intensity, intensity), 4);
              graphics.DrawLine(pen, x-2, y, x + 2, y);
            }
          }
        }
      }
    }
    */

    public override void Draw()
    {
      //coordinate system
      DebugDrawingTool.CreateLine("world:_map", new Vector2(-800, 0), new Vector2(800, 0), 3, Color.Gray);
      DebugDrawingTool.CreateLine("world:_map", new Vector2(0, -800), new Vector2(0, 800), 3, Color.Gray);

      if (DebugKeyTool.IsActive("options view:map - hide"))
      {
        DebugDrawingTool.CreateRectangle("world:_map", 
          new Vector2(-widthOfMap / 2, -heightOfMap / 2),
          new Vector2(+widthOfMap / 2, +heightOfMap / 2), 1, Color.Black);
      }
      else
      {
//        graphics.DrawImage(bitmap, -widthOfMap / 2, -heightOfMap / 2);
      }

      if (DebugKeyTool.IsActive("options view:map - pixel wise"))
      {
        for (int x = -500; x < 500; x+=4)
        {
          for (int y = -500; y < 500; y+=4)
          {
            int intensity = (int)GetDepth(x, y);
            if (intensity != 0)
            {
              DebugDrawingTool.CreateLine("world:_map pixel wise",
               new Vector2(x - 2, y), 
               new Vector2(x + 2, y), 4, Color.FromArgb(intensity, intensity, intensity));
            }
          }
        }
      }
    }// draw


  }
}
